package com.petdog.infrastructure.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.petdog.infrastructure.entity.Pet;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 宠物Mapper接口
 */
@Mapper
public interface PetMapper extends BaseMapper<Pet> {
    /**
     * 根据宠物ID查询宠物
     */
    @Select("SELECT * FROM pet WHERE pet_id = #{petId} AND status = 1")
    Pet selectById(@Param("petId") Long petId);
    
    /**
     * 根据用户ID查询宠物列表
     */
    @Select("SELECT * FROM pet WHERE user_id = #{userId} AND status = 1 ORDER BY create_time DESC")
    List<Pet> selectByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID分页查询宠物列表
     */
    @Select("SELECT * FROM pet WHERE user_id = #{userId} AND status = 1 ORDER BY create_time DESC LIMIT #{offset}, #{pageSize}")
    List<Pet> selectByUserIdWithPage(@Param("userId") Long userId, @Param("offset") int offset, @Param("pageSize") int pageSize);
    
    /**
     * 查询用户宠物总条数
     */
    @Select("SELECT COUNT(*) FROM pet WHERE user_id = #{userId} AND status = 1")
    int selectCountByUserId(@Param("userId") Long userId);
    
    /**
     * 插入宠物
     */
    @Insert("INSERT INTO pet (user_id, name, breed, breed_info_id, gender, birthday, avatar, description, weight, " +
            "personality, health_condition, likes, dislikes, create_time, update_time, status) " +
            "VALUES (#{userId}, #{name}, #{breed}, #{breedInfoId}, #{gender}, #{birthday}, #{avatar}, #{description}, " +
            "#{weight}, #{personality}, #{healthCondition}, #{likes}, #{dislikes}, NOW(), NOW(), 1)")
    @Options(useGeneratedKeys = true, keyProperty = "petId")
    int insert(Pet pet);
    
    /**
     * 更新宠物
     */
    @Update("UPDATE pet SET name = #{name}, breed = #{breed}, breed_info_id = #{breedInfoId}, gender = #{gender}, " +
            "birthday = #{birthday}, avatar = #{avatar}, description = #{description}, weight = #{weight}, " +
            "personality = #{personality}, health_condition = #{healthCondition}, likes = #{likes}, " +
            "dislikes = #{dislikes}, status = #{status}, update_time = NOW() WHERE pet_id = #{petId}")
    int update(Pet pet);
    
    /**
     * 删除宠物（逻辑删除）
     */
    @Update("UPDATE pet SET status = 0, update_time = NOW() WHERE pet_id = #{petId}")
    int delete(@Param("petId") Long petId);
    
    /**
     * 根据社区ID查询所有成员的宠物
     */
    List<Pet> selectByCommunityId(Long communityId);
    
    /**
     * 分页查询宠物列表
     */
    List<Pet> selectPage(Map<String, Object> params);
    
    /**
     * 查询宠物详情
     */
    Pet selectDetail(Long petId);
}
